﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Pub_Module_J_TopProduct : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindData();

    }
    private void BindData()
    {
        string viewcount = System.Configuration.ConfigurationManager.AppSettings["TopPD"];

        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_Top_ProductNoneProc(viewcount);
        dtlThumbnail.DataSource = ds;
        dtlThumbnail.DataBind();

        if (dtlThumbnail.Items.Count > 0)
        {
            for (int i = 0; i < dtlThumbnail.Items.Count; i++)
            {
                if (i + 1 % 3 == 0)
                {
                    HtmlTableCell td_right = (HtmlTableCell)dtlThumbnail.Items[i].FindControl("td_right");
                    td_right.Visible = false;
                }
                if (i == 0 || i == 1 || i == 2)
                {
                    HtmlTableCell td_top = (HtmlTableCell)dtlThumbnail.Items[i].FindControl("td_top");
                    td_top.Visible = true;
                }

            }
        }


    }

    protected void dtlThumbnail_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        string img = ResolveUrl("~/") + DataBinder.Eval(e.Item.DataItem, "ImgUrl").ToString();
        if (DataBinder.Eval(e.Item.DataItem, "ImgUrl").ToString() == "" || DataBinder.Eval(e.Item.DataItem, "ImgUrl") == null)
            img = ResolveUrl("~/") + "Images/Body/Noimages.gif";


        string HTML = "";

        HTML += "<a href='" + img + "'  class='customGal' title='" + DataBinder.Eval(e.Item.DataItem, "PDName").ToString() + "'>";
        HTML += "<img  src='" + img + "'  alt='' title='Click to enlarge' border='0'  class='img_Product' >";
        HTML += "</a>";

        Literal ltlImages = (Literal)e.Item.FindControl("ltlImages");
        ltlImages.Text = HTML;

        Image imgHot = (Image)e.Item.FindControl("imgHot");
        if (DataBinder.Eval(e.Item.DataItem, "IsHot").ToString() == "Y")
        {
            imgHot.Visible = true;
            imgHot.ImageUrl = "~/Images/Icon/hot.gif";
        }
        if (DataBinder.Eval(e.Item.DataItem, "KindNo").ToString() == "F")
        {
            Panel pnlKindNo = (Panel)e.Item.FindControl("pnlKindNo");
            pnlKindNo.Visible = false;
        }

        HtmlImage btnISelect = (HtmlImage)e.Item.FindControl("btnISelect");
        HtmlImage btnICancel = (HtmlImage)e.Item.FindControl("btnICancel");

        Label lblDongia = (Label)e.Item.FindControl("lblDongia");
        lblDongia.Text = FormatMoney(lblDongia.Text);

        //Discount
        string Price = DataBinder.Eval(e.Item.DataItem, "Price").ToString();
        string Rate = DataBinder.Eval(e.Item.DataItem, "Rate").ToString();
        HtmlTableCell tdPrice = (HtmlTableCell)e.Item.FindControl("tdPrice");
        if (DataBinder.Eval(e.Item.DataItem, "IsSaleOf").ToString() == "Y")
        {
            HtmlTableRow trSaleOf = (HtmlTableRow)e.Item.FindControl("trSaleOf");
            trSaleOf.Visible = true;

            Label lblDiscountPrice = (Label)e.Item.FindControl("lblDiscountPrice");
            lblDiscountPrice.Text = FormatMoney(lblDiscountPrice.Text);

            tdPrice.Attributes.Add("class", "td_linethrought");

            //get price discount
            Price = DataBinder.Eval(e.Item.DataItem, "DiscountPrice").ToString();
            Rate = DataBinder.Eval(e.Item.DataItem, "DiscountRate").ToString();
        }
        if (DataBinder.Eval(e.Item.DataItem, "Status").ToString() == "C")
        {
            tdPrice.Attributes.Add("class", "td_linethrought");
        }

        if (Session["dtBasket"] != null)
        {
            DataTable dtBasket = (DataTable)Session["dtBasket"];
            if (SearchDataTable(dtBasket, "PDID", DataBinder.Eval(e.Item.DataItem, "PDID").ToString()) != -1) //Neu ton tai san pham nay hien nut cancel
            {
                btnISelect.Style.Add("display", "none");
                btnICancel.Style.Add("display", "block");

            }
        }

    }

    protected void dtlThumbnail_ItemCommand(object source, DataListCommandEventArgs e)
    {

        DataTable dtBasket = new DataTable();

        if (Session["dtBasket"] != null)
        {
            dtBasket = (DataTable)Session["dtBasket"];
        }
        else
        {
            dtBasket.Columns.Add("PDID");
            dtBasket.Columns.Add("PDNAME");
            dtBasket.Columns.Add("PRICE");
            dtBasket.Columns.Add("QUANTITY");
            dtBasket.Columns.Add("RATE");
            dtBasket.Columns.Add("KINDNO");
        }

        if (e.CommandName.ToLower() == "addbasket")
        {
            string[] strArgument = e.CommandArgument.ToString().Split('☼');
            string strPDID = strArgument[0];
            string strPDName = strArgument[1];
            string strPDPrice = strArgument[2];
            string strPDRate = strArgument[3];
            string strPDKindNo = strArgument[4];

            clsWebShop cls = new clsWebShop();
            cls.UpdateViewCount(strPDID);

            DataRow row = dtBasket.NewRow();
            row["PDID"] = strPDID;
            row["PDNAME"] = strPDName;
            row["PRICE"] = strPDPrice;
            row["QUANTITY"] = "1";
            row["RATE"] = strPDRate;
            row["KINDNO"] = strPDKindNo;
            dtBasket.Rows.Add(row);

            Session["dtBasket"] = dtBasket;
            string url = Request.Url.ToString() + "?#PageIndex_" + strPDID;
            Response.Redirect(url);


        }
        else if (e.CommandName.ToLower() == "cancelbasket")
        {
            string PDID = e.CommandArgument.ToString();
            clsCommon clsCom = new clsCommon();
            int index = clsCom.SearchDataTable(dtBasket, "PDID", PDID);

            if (index != -1)
            {
                dtBasket.Rows[index].Delete();
                string url = Request.Url.ToString() + "?#PageIndex_" + PDID;
                Response.Redirect(url);
            }
        }
        else if (e.CommandName.ToLower() == "detailbasket")
        {
            string PDID = e.CommandArgument.ToString();
            Response.Redirect("~/Shopping/Stall/ProductDetail.aspx?PDID=" + PDID);
        }


    }

    #region "CommonFunction"
    private string ReturnQueryString(string strQuery)
    {
        string str = "";
        if (Request.QueryString[strQuery] != null && Request.QueryString[strQuery] != "")
        {
            str = strQuery + "=" + Request.QueryString[strQuery];
        }
        return str;
    }
    public int SearchDataTable(DataTable dt, string Col, string value)
    {
        int index = -1;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i][Col].ToString() == value)
            {
                index = i;
                break;
            }
        }
        return index;
    }
    private string FormatMoney(string sNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", decimal.Parse(sNum));
        }
        catch (Exception) { }
        return sReturn;
    }


    private string FormatMoney(decimal dNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", dNum);
        }
        catch (Exception) { }
        return sReturn;
    }
    #endregion
}
